package com.lmy.springboot.controller;

import com.lmy.springboot.bean.Car;
import com.lmy.springboot.bean.Monster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class ResponseController {

    /**
     * 使用@ResponseBody注解，以json格式返回数据
     * 1.本质上@ResponseBody决定了响应体的ContentType
     * 2,返回数据时会调用AbstractJackson2HttpMessageConverter转换器，根据ContentType生成对应的generator生成器，将目标Object转换成需要返回的类型(如application/json;application/xml等)
     * @return
     */
    @GetMapping(value = "/getMonster")
    @ResponseBody
    public Monster getMonster() {
        Monster monster = new Monster();
        monster.setName("奔波霸");
        monster.setAge(200);
        monster.setId(100);
        monster.setBirth(new Date());
        monster.setIsMarried(false);
        Car car = new Car();
        car.setName("奔驰");
        car.setPrice(2000000.2);
        monster.setCar(car);
        return monster;
    }
}
